<html>
<head><meta charset="utf-8"><title>const fn do not have overflow checks · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html">const fn do not have overflow checks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229183674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20do%20not%20have%20overflow%20checks/near/229183674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html#229183674">(Mar 07 2021 at 12:24)</a>:</h4>
<p>The following program reports errors on FOO and BAR in debug mode, but only an error on BAR in release mode.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">FOO</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="n">sub</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="n">BAR</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">sub</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This seems a bit surprising from a user perspective imo. It's obvious from a rustc impl perspective, as we explicitly force overflow checks in constants, but can't do the same for const fn as they are also run at runtime. Should we change anything here? Keep it? We should at least document it somewhere</p>



<a name="230153546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20do%20not%20have%20overflow%20checks/near/230153546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html#230153546">(Mar 13 2021 at 10:24)</a>:</h4>
<p>how is the separate MIR for const fn implemented currently? I assume MIR building only runs once and then we make a copy? That would mean we cannot easily make the <code>const fn</code> MIR include overflow checks</p>



<a name="230153571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20do%20not%20have%20overflow%20checks/near/230153571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html#230153571">(Mar 13 2021 at 10:25)</a>:</h4>
<p>in that case I think I am in favor of consistency, and removing the overflow checks in release mode even in <code>const</code></p>



<a name="230153653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20do%20not%20have%20overflow%20checks/near/230153653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html#230153653">(Mar 13 2021 at 10:26)</a>:</h4>
<p>we could in principle have a flag in the CTFE machine to error on overflows, but taht'd be fragile since this code is shared with e.g. the wrapping_ intrinsics and since we need to b careful not to trigger this error in promoteds... I don't think I'd like this approach</p>



<a name="230153850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20do%20not%20have%20overflow%20checks/near/230153850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html#230153850">(Mar 13 2021 at 10:31)</a>:</h4>
<p>another alternative would be to change <code>Binop</code> to something like <code>Binop(BinopMode, Binop, ..)</code></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">BinopMode</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Checked</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">CTFEChecked</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">Unchecked</span><span class="p">,</span><span class="w"> </span><span class="c1">// unchecked is wrong, need a different name</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="230153892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20fn%20do%20not%20have%20overflow%20checks/near/230153892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20fn.20do.20not.20have.20overflow.20checks.html#230153892">(Mar 13 2021 at 10:32)</a>:</h4>
<p>which makes only erroring during ctfe fairly safe i think</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>